

package com.hexinfo.dmpro.script.publish.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.hexinfo.dmpro.script.publish.entity.ScriptPublishTaskEntity;
import com.hexinfo.dmpro.script.publish.service.IScriptPublishTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 脚本发布任务
 *
 * @author Wang zhihao
 * @date 2023-09-13 18:16:07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api/scriptPublish/task" )
@Api(value = "scriptPublishTask", tags = "脚本发布任务管理")
public class ScriptPublishTaskController {

    private final IScriptPublishTaskService scriptPublishTaskService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param scriptPublishTask 脚本发布任务
     * WrapperEnhancer.like(scriptPublishTask); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseEntity<Object> getScriptPublishTaskPage(Page page, ScriptPublishTaskEntity scriptPublishTask) {
        return new ResponseEntity<>(scriptPublishTaskService.queryList(page,scriptPublishTask), HttpStatus.OK);
    }


    /**
     * 通过id查询脚本发布任务
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseEntity<Object> getById(@PathVariable("id" ) String id) {
        return ResponseEntity.ok(scriptPublishTaskService.getById(id));
    }

    /**
     * 新增脚本发布任务
     * @param scriptPublishTask 脚本发布任务
     * @return R
     */
    @ApiOperation(value = "新增脚本发布任务", notes = "新增脚本发布任务")
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody ScriptPublishTaskEntity scriptPublishTask) {
        return ResponseEntity.ok(scriptPublishTaskService.save(scriptPublishTask));
    }

    /**
     * 修改脚本发布任务
     * @param scriptPublishTask 脚本发布任务
     * @return R
     */
    @ApiOperation(value = "修改脚本发布任务", notes = "修改脚本发布任务")
    @PutMapping
    public ResponseEntity<Object> updateById(@RequestBody ScriptPublishTaskEntity scriptPublishTask) {
        return ResponseEntity.ok(scriptPublishTaskService.updateById(scriptPublishTask));
    }

    /**
     * 通过id删除脚本发布任务
     * @param ids
     * @return R
     */
    @ApiOperation(value = "通过id删除脚本发布任务", notes = "通过id删除脚本发布任务")
    @DeleteMapping(value = {"/{ids}"})
    public ResponseEntity<Object> removeById(@PathVariable("ids") @NotNull List<String> ids) {
        scriptPublishTaskService.removeByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * 还原任务
     * @param
     * @return R
     */
    @ApiOperation(value = "还原", notes = "还原")
    @PostMapping(value = "/restore/{publishTaskId}/{clusterId}")
    public ResponseEntity<Object> restore(@PathVariable("publishTaskId") String publishTaskId, @PathVariable("clusterId") String clusterId) {
        scriptPublishTaskService.restore(publishTaskId, clusterId);
        return ResponseEntity.ok("还原成功");
    }

    /**
     * @Method upload
     * @Param
     * @param httpServletRequest
     * @Return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 上传
     * @Author Wang zhihao
     * @Date 2023/9/14 20:26
     * @Version V1.0
     */
    @ApiOperation(value = "上传", notes = "上传")
    @PostMapping(value = "/upload")
    public ResponseEntity<Object> upload(HttpServletRequest httpServletRequest) {
        List<MultipartFile> multipartFileList = ((MultipartHttpServletRequest) httpServletRequest).getFiles("fileList");
        return ResponseEntity.ok(scriptPublishTaskService.upload(multipartFileList));
    }

}
